import { Get, Post } from '@/utils/request'
import {
  languageVersion,
  queryAppParams,
  queryAppTaskBody,
  reqAddAppBody,
  reqAddChildDirBody,
  reqGetChildFilesParams,
  reqSavePreRunEnvBody,
  reqSaveTempBody,
  reqUploadFileByFileIdBody,
  respAppData,
  respAppDetailData,
  respAppDirData,
  respAppTaskData,
  respConfigKeysData,
} from './types/application'
import { respCommonList } from '@/api/types/common'

export const baseURL = import.meta.env.VITE_APP_BASE_URL

//项目用户相关的请求地址
enum API {
  APP_LIST_URL = '/bioinfor/application/findByPage',
  UPLOAD_FILE_BY_FILEID_URL = '/bioinfor/application/files/uploadFile',
  CREATE_APP_URL = '/bioinfor/application/addOrUpdateApplication',
  APP_ALL_DIR_URL = '/bioinfor/application/getApplicationFiles',
  APP_PUT_URL = '/bioinfor/application/updatePutStatus',
  DELETE_APP_URL = '/bioinfor/application/deleteApplication',
  APP_INFO_URL = '/bioinfor/application/getApplicationDetail',

  TEMP_DATA_DIR_URL = '/bioinfor/application/getTempDataDir',

  CREATE_CHILD_DIR_URL = '/bioinfor/application/files/uploadDir',
  CHILD_FILE_OR_DIR_URL = '/bioinfor/application/files/getDownLoadUrl',
  DELETE_FILE_URL = '/bioinfor/application/files/deleteFileByName',
  DELETE_DIR_URL = '/bioinfor/application/files/deleteDir',

  SAVE_TEMP_DATA_URL = '/bioinfor/application/saveTempData',
  TEMP_DATA_URL = '/bioinfor/application/getTempDataList',

  COMMON_CONFIG_KEYS_URL = '/sys/config/getAllKeys',
  COMMON_CONFIG_CONTENT_URL = '/sys/config/getContent',

  APP_PRE_RUN_ENV_URL = '/bioinfor/application/getAppPreRunEnv',
  APP_PRE_RUN_ENV_SUBMIT_URL = '/bioinfor/application/operate/submitPreRunEnv',
  APP_RUN_LOG_URL = '/bioinfor/application/run/getAppRunningLogs',
  APP_DOWNLOAD_LOG_URL = '/bioinfor/application/operate/downLoadAppRunningLogs',
  APP_ADD_TO_OUT_COME_URL = '/bioinfor/application/run/addToOutCome',
  DELETE_APPLICATION_RUN_URL = '/bioinfor/application/run/deleteApplicationRun',

  APP_TASK_URL = '/bioinfor/application/run/findList',
}

//获取应用列表
export const reqgetAppTaskList = (reqData: queryAppParams) =>
  Post<respCommonList<respAppData[]>>(API.APP_LIST_URL, reqData)

// 添加应用
export const reqCreateApp = (reqData: reqAddAppBody) =>
  Post<respCommonList<string>>(API.CREATE_APP_URL, reqData)

//应用详情
export const reqGetAppInfo = (appId: number) =>
  Get<respAppDetailData>(API.APP_INFO_URL + `/${appId}`)
//应用发布
export const reqAppPut = (appId: number) =>
  Get<string>(API.APP_PUT_URL + `/${appId}`)

//删除应用
export const reqDeleteApp = (appId: number) =>
  Get<string>(API.DELETE_APP_URL + `/${appId}`)

// 获取应用文件夹
export const reqGetAppAllDirList = (appId: number) =>
  Get<respAppDirData[]>(API.APP_ALL_DIR_URL + `/${appId}`)

export const reqUploadFileByFileId = (reqData: reqUploadFileByFileIdBody) =>
  Post<string>(
    API.UPLOAD_FILE_BY_FILEID_URL,
    reqData,
    {},
    { 'Content-Type': 'multipart/form-data' },
  )

// 创建子文件夹
export const reqCreateChildDir = (reqData: reqAddChildDirBody) =>
  Post(API.CREATE_CHILD_DIR_URL, {}, reqData)

// 获取子文件或文件夹
export const reqGetFilesOrDirsByFileId = (reqData: reqGetChildFilesParams) =>
  Post<respAppDirData[]>(API.CHILD_FILE_OR_DIR_URL, reqData)

// 删除文件
export const reqDeleteFile = (reqData: reqGetChildFilesParams) =>
  Post<string | null>(API.DELETE_FILE_URL, reqData)

//刪除文件夾
export const reqDeleteDirByFileId = (fileId: number) =>
  Get<string>(API.DELETE_DIR_URL + `/${fileId}`)

// 保存临时文件
export const reqSaveTempData = (reqData: reqSaveTempBody) =>
  Post<string>(API.SAVE_TEMP_DATA_URL, reqData)

// 查询临时文件
export const reqGetTempDataList = (appId: number) =>
  Get(API.TEMP_DATA_URL + `/${appId}`)

// 获取预装软件包
export const reqGetAppPreRunEnv = (appId: number) =>
  Get<string[]>(API.APP_PRE_RUN_ENV_URL + `/${appId}`)

// 提交预装类库
export const reqSavePreRunEnv = (reqData: reqSavePreRunEnvBody) =>
  Post<string[]>(API.APP_PRE_RUN_ENV_SUBMIT_URL, reqData)

// 查看运行日志
export const reqGetAppRunLogData = (runId: number) =>
  // Post<string[]>(API.APP_RUN_LOG_URL + `?applicationId=${appId}`)
  Get(API.APP_RUN_LOG_URL + `/${runId}`)

// 下载运行日志
export const reqDownAppRunLogData = (appId: number) =>
  Post<string>(API.APP_DOWNLOAD_LOG_URL + `?applicationId=${appId}`)

export const reqGetConfigKeys = () =>
  Get<respConfigKeysData[]>(API.COMMON_CONFIG_KEYS_URL)

export const reqGetConfigContent = (key: string) =>
  Post<languageVersion[] | respConfigKeysData[]>(
    API.COMMON_CONFIG_CONTENT_URL + '?key=' + key,
  )

// 获取任务列表
export const reqGetAppTaskList = (reqData: Partial<queryAppTaskBody> = {}) =>
  Post<respCommonList<respAppTaskData[]>>(API.APP_TASK_URL, reqData)

// 添加到成果流
export const reqAppAddToOutCome = (runId: number) =>
  Post<string[]>(API.APP_ADD_TO_OUT_COME_URL + '?runId=' + runId)
// 删除任务
export const reqDeleteApplicationRun = (runId: number) =>
  Get(API.DELETE_APPLICATION_RUN_URL + '/' + runId)

// 预运行
export const reqPreApplicationRun = (appId: number) =>
  Get('/bioinfor/application/operate/preRunApplication/' + appId)

// App运行
export const reqApplicationRun = (reqData: any) =>
  Post<string>('/bioinfor/application/operate/runApplication', reqData)

// App运行Demo
export const reqAppCopyDemoRun = (reqData: any) =>
  Post<string>('/bioinfor/application/task/copyDemo', reqData)

// 运行状态
export const reqGetAppRunningStatus = (runId: number) =>
  Get('/bioinfor/application/run/getAppRunningStatus/' + runId)

// 单文件上传（需要传递表名）
// (reqData: reqUploadFileByFileIdBody) =>
export const reqUploadSingle = (reqData: any) =>
  Post<string>(
    '/bioinfor/application/task/uploadSingle',
    reqData,
    {},
    { 'Content-Type': 'multipart/form-data' },
  )

// 文件夹上传（需要传递表名）
// (reqData: reqUploadFileByFileIdBody) =>
export const reqUpdateMulti = (reqData: any) =>
  Post<string>(
    '/bioinfor/application/task/updateMulti',
    reqData,
    {},
    { 'Content-Type': 'multipart/form-data' },
  )

// 打开文件夹App运行结果文件夹
export const reqAppOpenResultFiles = (reqData: any) =>
  Post<string>('/bioinfor/application/run/openResultFiles', reqData)

// 打开文件夹App运行结果文件夹(所有文件)
export const reqAppALLResultFiles = (runId: any) =>
  Post<string>('/bioinfor/application/run/getAllRunFile?runId=' + runId)

// 下载运行日志
export const reqAppDownLoadRunLog = (runId: any) =>
  Get('/bioinfor/application/run/downLoadRunLog/' + runId)
